{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31a63d07-866d-4883-83fc-0fa453dc9775",
   "metadata": {},
   "source": [
    "# Subtraction\n",
    "\n",
    "Subtraction (denoted as '-') is implemented by negation and addition in 2-complement representation.\n",
    "\n",
    "$$\n",
    "a - b \\longleftrightarrow a + (-b) \\longleftrightarrow a + \\sim{b} + lsb\\_value\n",
    "$$\n",
    "\n",
    "Where '~' is bitwise not and $lsb\\_value$ is the least significant bit value.\n",
    "\n",
    "Note that integer and fixed-point numbers are represented in a 2-complement method during function evaluation.\n",
    "The binary number is extended in the case of a register size miss-match.\n",
    "For example, the positive signed number $(110)_2=6$ is expressed as $(00110)_2$ when operating with a 5-qubit register.\n",
    "Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.\n",
    "\n",
    "Examples:\n",
    "\n",
    "5 + 3 = 8 , 0101 + 0011 = 1000\n",
    "\n",
    "5 - 3 = 5 + (-3) = 2, 0101 + 1101 = 0010\n",
    "\n",
    "-5 + -3 = -8, 1011 + 1101 = 1000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25b2abef-2324-4dd4-bebc-7e60b44e7caa",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "#### Example 1: Subtraction of Two Quantum Variables\n",
    "\n",
    "This example generates a quantum program that subtracts one quantum variables from the other, both of size 3 qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85eb57a6-0488-49a5-8675-5cb4c94689bb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:00.765337Z",
     "iopub.status.busy": "2024-05-07T13:23:00.764740Z",
     "iopub.status.idle": "2024-05-07T13:23:03.884713Z",
     "shell.execute_reply": "2024-05-07T13:23:03.884033Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    prepare_int(4, a)\n",
    "    prepare_int(5, b)\n",
    "    res |= a - b\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc11dc7d-eb99-4042-ba3b-c0aebaee66b6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:03.887616Z",
     "iopub.status.busy": "2024-05-07T13:23:03.887167Z",
     "iopub.status.idle": "2024-05-07T13:23:06.612390Z",
     "shell.execute_reply": "2024-05-07T13:23:06.611645Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 4.0, 'b': 5.0, 'res': -1.0}: 1000]\n"
     ]
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"subtraction_2vars_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "print(result.parsed_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d26be91f-435f-4f0d-8214-1b5b06196dbd",
   "metadata": {},
   "source": [
    "#### Example 2: Subtraction of a Float from a Register\n",
    "\n",
    "This example generates a quantum program which subtracts two argument. The left_arg is defined to be a fix point number $(11.1)_2$ (3.5).\n",
    "The right_arg is defined to be a quantum register of size of three."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe79e229-397b-4ee9-8396-0831e809008a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:06.616814Z",
     "iopub.status.busy": "2024-05-07T13:23:06.616087Z",
     "iopub.status.idle": "2024-05-07T13:23:06.625937Z",
     "shell.execute_reply": "2024-05-07T13:23:06.625213Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
    "    prepare_int(4, a)\n",
    "    res |= a - 3.5\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f8704a73-2dd0-4343-bf5a-cde4e14dffdc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:06.629036Z",
     "iopub.status.busy": "2024-05-07T13:23:06.628788Z",
     "iopub.status.idle": "2024-05-07T13:23:09.055239Z",
     "shell.execute_reply": "2024-05-07T13:23:09.054590Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 4.0, 'res': 0.5}: 1000]\n"
     ]
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"subtraction_float_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "print(result.parsed_counts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
